package servlet;

import dao.ProductDAO;
import entity.Product;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

@WebServlet(name = "productAdd", value = "/product/add")
public class ProductAddServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        //接收请求参数
        String name=req.getParameter("name");
        String price=req.getParameter("price");
        String sum=req.getParameter("sum");
        String info= req.getParameter("info");
        String imageUrl= req.getParameter("imageUrl");
        //System.out.println("imageUrl: " + imageUrl);
        //转化数据类型
        double price1=Double.parseDouble(price.trim());
        long sum1=Long.parseLong(sum.trim());
        //封装成Product对象
        Product p=new Product();
        p.setName(name);
        p.setPrice(price1);
        p.setSum(sum1);
        p.setInfo(info);
        p.setAddDate(new Date());
        p.setImageUrl(imageUrl);
        p.setSaleCount(0L);
        p.setVisitCount(0L);
        //System.out.println(p);
        //调用dao
        ProductDAO productDao=new ProductDAO();
        productDao.insert(p);
        resp.sendRedirect("/Mall/product/list");

    }
}
